$(function () {
    var tale = new $.tale();
    getinfo(1);
    $("#filter").click(function () {
        getinfo(1);
    })
    $("#addfriends").click(function () {
        var name=$("#friendsname").val();
        var url=$("#friendsurl").val();
        if(name==''||url==''){
            tale.alertWarn("请输入友链名称或链接!");
            return;
        }
        $("#tagsmodel1").modal('hide');
        $.post({
            url:baseurl+api.friends.add,
            xhrFields: {
                withCredentials: true
            },
            data:{name: name,url:url},
            success: function (res) {
                $("#friendsname").val("");
                $("#friendsurl").val("");
                tale.alertOk(res.message);
                getinfo(1);
            },
            datatype: "json"
        })

    })
    $("#updatefriends").click(function () {
        var id=$("#friendsid").val();
        var name=$("#friendsname2").val();
        var url=$("#friendsurl2").val();
        if(name==''||url==''){
            tale.alertWarn("请输入友链名称或链接!");
            return;
        }
        $("#tagsmodel2").modal('hide');
        $.post({
            url:baseurl+api.friends.update,
            xhrFields: {
                withCredentials: true
            },
            data:{id:id,name: name,url:url},
            success: function (res) {
                $("#friendsname").val("");
                $("#friendsurl").val("");
                tale.alertOk(res.message);
                getinfo(1);
            },
            datatype: "json"
        })
    })
})

function getinfo(pagenum) {
    //页面加载完后，发出ajax请求
    var name=$("#name").val();
    var url=$("#url").val();
    $.post({
        url: baseurl+api.friends.base+pagenum,
        data: {name: name,url: url},
        xhrFields: {
            withCredentials: true
        },
        success: function (res) {
            var s1="";
            var friendsInfo=res.data.friendsInfo;
            var pageInfo=res.data.pageinfo;
            for (var i = 0; i < friendsInfo.length; i++) {
                s1+=" <tr style='vertical-align: middle;text-align: center;'>\n" +
                    "                            <td>"+friendsInfo[i].id+"</td>\n" +
                    "                            <td>"+friendsInfo[i].name+"</td>\n" +
                    "                            <td><a href='"+friendsInfo[i].url+"' target='_blank'>"+friendsInfo[i].url+"</a></td>\n" +
                    "                            <td colspan=\"6\"><button class=\"btn btn-primary\" onclick=\"openedit("+friendsInfo[i].id+",'"+friendsInfo[i].name+"','"+friendsInfo[i].url+"')\">编辑</button>&nbsp;<button class=\"btn-danger btn\" onclick=\"deletefriends("+friendsInfo[i].id+")\">删除</button></td>\n" +
                    "                        </tr>"
            }
            $("#tbody").html(s1);
            $("#current-page").html("第"+pageInfo.pageNum+"页");
            if(!pageInfo.hasPreviousPage){
                $("#first").attr("class","disabled");
                $("#pre").attr("class","disabled");
                $("#first").html("<a href=\"javascript:void(0);\">首页</a>");
                $("#pre").html("<a href=\"javascript:void(0);\" >上一页</a>");
            }else{
                $("#first").removeAttr("class","disabled");
                $("#pre").removeAttr("class","disabled");
                $("#first").html("<a href=\"javascript:void(0);\" onclick=\"getinfo(1)\">首页</a>");
                $("#pre").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageInfo.prePageNum+")\">上一页</a>");
            }
            if(!pageInfo.hasNextPage){
                $("#next").attr("class","disabled");
                $("#last").attr("class","disabled");
                $("#next").html("<a href=\"javascript:void(0);\">下一页</a>");
                $("#last").html("<a href=\"javascript:void(0);\" >尾页</a>");
            }else{
                $("#next").removeAttr("class","disabled");
                $("#last").removeAttr("class","disabled");
                $("#next").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageInfo.nextPageNum+")\">下一页</a>");
                $("#last").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageInfo.totalpage+")\">尾页</a>");
            }
            $("#total").html(pageInfo.totalpage);
        },
        datatype: "json"
    })
}
function transferto() {
    var tale=new $.tale();
    var pagenum=$("#pagenum").val();
    var totalpage=$("#total").html();
    if(pagenum<1||pagenum>totalpage||pagenum==''){
        tale.alertWarn("页码无效!");
        return;
    }
    getinfo(pagenum);
}
function deletefriends(id) {
    swal({
        title: '确定删除吗?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定',
        cancelButtonText:"取消",
    }).then(function(isConfirm) {
        if (isConfirm) {
            $.post({
                url: baseurl+api.friends.delete+id,
                xhrFields: {
                    withCredentials: true
                },
                success:function (res) {
                    swal("删除成功!");
                    getinfo(1);
                }
            })
        }
    })
}
function openedit(id,name,url) {
    $("#tagsmodel2").modal('show');
    $("#friendsid").val(id);
    $("#friendsname2").val(name);
    $("#friendsurl2").val(url);
}